package com.kunbo.cn.controller.core.other;

import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kunbo.cn.enumeration.core.logistics.DriverState;
import com.kunbo.cn.util.retu.CommonResult;

@RestController
@RequestMapping("/other/")
public class OtherController {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Transactional
	@RequestMapping("updateCarAndDriverState")
	public CommonResult updateCarAndDriverState(String idCard,String carNumber) {
		if(StringUtils.isBlank(idCard)) {
			return new CommonResult(false,"身份证号不可为空",null);
		}
		if(StringUtils.isEmpty(carNumber)) {
			return new CommonResult(false,"车牌号不可为空",null);
		}
		String driverSql = "UPDATE driver_management set driver_state = '"+DriverState.occupy.getName()+"' where idcrad = '"+idCard+"';";
		String carSql = "update vehicle_management set car_state = '"+DriverState.occupy.getName()+"' where license_plate_number = '"+carNumber+"'";
		int a = jdbcTemplate.update(driverSql);
		int b = jdbcTemplate.update(carSql);
		if(a+b==2) {
			return new CommonResult(true,"更新成功",null);
		}
		return null;
		
		
	}
	//IC卡注册时获取所有车辆接口
	@RequestMapping("getAllCar")
	public CommonResult getAllCar(String carNumber) {
		String carSql = "select id,license_plate_number from vehicle_management";
		if(StringUtils.isNotBlank(carNumber)) {
			carSql+=" where license_plate_number like'%"+carNumber+"%'";
		}
		List<Map<String,Object>> list  = jdbcTemplate.queryForList(carSql);
		
		return new CommonResult(true,"获取成功",list);
		
	}

}
